package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.StudentDto;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {
    @Autowired
    StudentService studentService;

    @GetMapping
    public R pageGet(StudentDto dto) {
        PageBean bean = studentService.pageGet(dto);
        return R.ok(bean);
    }

    @DeleteMapping("/{ids}")
    public R deteleByIds(@PathVariable Integer[] ids) {
        log.info("删除学员", ids);
        studentService.deteleByIds(ids);
        return R.ok();

    }

    @PostMapping
    public R insert(@RequestBody Student student) {
        log.info("添加学员：{}", student);
        studentService.insert(student);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        Student student = studentService.getById(id);
        return R.ok(student);
    }

    @PutMapping
    public R updateById(@RequestBody Student student) {
        log.info("修改学员：{}", student);
        studentService.updateById(student);
        return R.ok();
    }

    @PutMapping("violation/{id}/{score}")
    public R updateByCount(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("违纪处理：{},{}", id, score);
        studentService.updateByCount(id,score);
        return R.ok();
    }
}
